package com.maven.front.controller.eduQuestionsTag;

import com.maven.front.bean.EduQuestionsTO;
import com.maven.front.bean.EduQuestionsTag;
import com.maven.front.bean.Result;
import com.maven.front.service.eduQuestionsTag.EduQuestionsTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("admin/edutag")
public class EduQuestionsTagController {

    @Autowired
    private EduQuestionsTagService eduQuestionsTagService;



    @GetMapping("listAll")
    public Result list(EduQuestionsTag eduQuestionsTag){
        List<EduQuestionsTag> list=eduQuestionsTagService.list(eduQuestionsTag);

        if (CollectionUtils.isEmpty(list)){
            return new Result(1009,"失败");
        }else {
            return new Result(200,"成功",list);
        }
    }


    //树形图
    @GetMapping("listTo")
    public Result listTo(EduQuestionsTag eduQuestionsTag){
        List<EduQuestionsTag> list=eduQuestionsTagService.listTo(eduQuestionsTag);
        List<EduQuestionsTO> treevo=new ArrayList<>();
        for(EduQuestionsTag b:list){
            EduQuestionsTO eduTO=new EduQuestionsTO();
            eduTO.setId(b.getQuestionsTagId());
            eduTO.setName(b.getQuestionsTagName());
            eduTO.setpId(b.getParentId());
            treevo.add(eduTO);
        }
        if (CollectionUtils.isEmpty(list)){
            return new Result(1009,"失败");
        }else {
            return new Result(200,"成功",treevo);
        }
    }

    //新增
    @GetMapping("save")
    public Result save(EduQuestionsTag eduQuestionsTag){
        eduQuestionsTag.setCreateTime(new Date());
        eduQuestionsTag.setParentId(0);
        eduQuestionsTag.setStatus(0);
        int a=eduQuestionsTagService.save(eduQuestionsTag);
        if (a>0){
            return new Result(1009,"失败");
        }else {
            return new Result(200,"成功");
        }

    }

    //删除
    @GetMapping("delete")
    public Result delete(Integer id){
        System.out.println(id);
        int a=eduQuestionsTagService.delete(id);
        if (a>0){
            return new Result(1009,"失败");
        }else {
            return new Result(200,"成功");
        }
    }

    //修改
    @PostMapping("update")
    public Result update(EduQuestionsTag eduQuestionsTag){
        System.out.println(eduQuestionsTag.getQuestionsTagName());
        eduQuestionsTag.setCreateTime(new Date());
        int a=eduQuestionsTagService.update(eduQuestionsTag);
        if (a>0){
            return new Result(1009,"失败");
        }else {
            return new Result(200,"成功");
        }

    }



}
